package com.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dto.OrdersDto;
import com.pojo.*;
import com.service.*;
import com.util.BaseContext;
import com.util.R;
import org.apache.logging.log4j.util.Strings;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Collectors;


@RestController
@RequestMapping("/order")
public class OrdersController {

    @Autowired
    private OrdersService ordersService;

    @Autowired
    private ShoppingCartService shoppingCartService;

    @Autowired
    private OrderDetailService orderDetailService;

    @Autowired
    private AddressBookService addressBookService;

    @Autowired
    private EmployeeService employeeService;

    @GetMapping("/page")
    public R<Page> page(int page, int pageSize, String number, LocalDateTime orderTime, LocalDateTime checkoutTime){
        Page<Orders> page1 = new Page<>(page, pageSize);
        LambdaQueryWrapper<Orders> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(Strings.isNotEmpty(number),Orders::getNumber,number);
        wrapper.eq(orderTime==LocalDateTime.now(),Orders::getOrderTime,orderTime);
        wrapper.eq(checkoutTime==LocalDateTime.now(),Orders::getCheckoutTime,checkoutTime);
        ordersService.page(page1,wrapper);
        return R.success(page1);
    }

    @GetMapping("/userPage")
    public R<Page> userpage(int page,int pageSize){
        Page<Orders> page1 = new Page<>(page, pageSize);
        LambdaQueryWrapper<Orders> wrapper = new LambdaQueryWrapper<>();
        wrapper.orderByDesc(Orders::getOrderTime);
        ordersService.page(page1,wrapper);
        return R.success(page1);
    }

    @PostMapping("/submit")
    public R<String> addOrder(@RequestBody OrdersDto ordersDto){
        //Long aLong = (Long) request.getSession().getAttribute("employee");
        Long aLong = BaseContext.getCurrentId();
        Employee one1 = employeeService.getById(aLong);
        LambdaQueryWrapper<AddressBook> wrapper1 = new LambdaQueryWrapper<>();
        wrapper1.eq(AddressBook::getUserId,aLong);
        wrapper1.eq(AddressBook::getIsDefault,1);
        AddressBook one = addressBookService.getOne(wrapper1);
        LambdaQueryWrapper<ShoppingCart> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ShoppingCart::getUserId,aLong);
        List<ShoppingCart> list = shoppingCartService.list(wrapper);
        long id = IdWorker.getId();
        AtomicInteger amount = new AtomicInteger();
        List<OrderDetail> list1=list.stream().map((item)->{
            OrderDetail detail = new OrderDetail();
            BeanUtils.copyProperties(item,detail);
            detail.setOrderId(id);
            amount.addAndGet(item.getAmount().multiply(new BigDecimal(item.getNumber())).intValue());
            return detail;
        }).collect(Collectors.toList());
        ordersDto.setId(id);
        ordersDto.setPhone(one.getPhone());
        ordersDto.setConsignee(one.getConsignee());
        ordersDto.setUserId(aLong);
        ordersDto.setOrderTime(LocalDateTime.now());
        ordersDto.setCheckoutTime(LocalDateTime.now());
        ordersDto.setAddress(one.getDetail());
        ordersDto.setUserName(one1.getName());
        ordersDto.setNumber(String.valueOf(id));
        ordersDto.setAmount(new BigDecimal(amount.get()));
        ordersService.save(ordersDto);
        orderDetailService.saveBatch(list1);
        shoppingCartService.remove(wrapper);
        return R.success("下单成功");
    }
}
